package Thread;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class ChessThreadsend extends Thread {
	private String ip;
	private int chesstype;
	private int fromx;
	private int fromy;
	private int tox;
	private int toy;
	private DatagramSocket client = null;
	public ChessThreadsend(String ip,int chesstype,int fromx,int fromy,int tox,int toy){
		this.ip = ip;
		this.chesstype = chesstype;
		this.fromx = fromx;
		this.fromy = fromy;
		this.tox = tox;
		this.toy = toy;
	}
	public void run(){
		try {
			 client = new DatagramSocket(9998);
		} catch (SocketException e1) {
		}
		ByteArrayOutputStream baos=new ByteArrayOutputStream();		
		ObjectOutputStream dos = null;
		int datas = 10000*chesstype+1000*fromx+100*fromy+10*tox+toy;
		try {
			dos = new ObjectOutputStream(new BufferedOutputStream(baos));
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		try {
			dos.writeInt(datas);
			dos.flush();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		

		byte[] data =baos.toByteArray();
		
		DatagramPacket packet=new DatagramPacket(data,0,data.length,new InetSocketAddress(ip,6667));
		try {
			client.send(packet);
		} catch (IOException e) {
			e.printStackTrace();
		}
		client.close();		
	}

}

